#### replication file for Military policy preferences of a conflict-dependent country corded by Hamanaka and Harada ####

### パスを指定
#getwd()
#setwd("")
rm(list=ls())

### データの読み込み(任意のパスから)
dedn <- haven::read_dta("diversionary_experiment_data_new.dta")


### パッケージ
library("tidyverse")


### データの加工
dedn_analysis <- data.frame(dedn %>% select(type, rally))


### ミサイルシナリオ内の比較(t検定結果)

## データの加工
supportPM1 <- dedn_analysis %>% 
  group_by(type) %>%
  summarise(
    support_mean = mean(rally, na.rm = TRUE),
    support_upper = mean(rally, na.rm = TRUE) + 1.96 * sd(rally, na.rm = TRUE) / sqrt(n()),
    support_lower = mean(rally, na.rm = TRUE) - 1.96 * sd(rally, na.rm = TRUE) / sqrt(n())
  ) %>% slice(-5:-9) %>% mutate(scenario = c("Missile_Irondome", "Missile_Drone", "Missile_IDF", "Missile_Regret"))

## ggplot
supportPM1 %>% ggplot(aes(as.character(type),
                         support_mean, label = round(support_mean, 2))) +
  geom_point() +
  geom_text(vjust = 0.5, hjust = -0.2, show.legend = FALSE, size = 3.5) + 
  geom_errorbar(aes(ymin = support_lower, ymax = support_upper), width = 0.1) +
  scale_x_discrete(labels = c("1" = "Missile_Irondome",
                              "2" = "Missile_Drone",
                              "3" = "Missile_IDF",
                              "4" = "Missile_Regret")) +
  scale_y_continuous(breaks = seq(0, 5.0, 0.5), limits = c(2.25, 4.25)) +
  theme_bw() +
  annotate("text", x=1.5, y=2.95, label="(<0.001)***") +
  annotate("text", x=1.47, y=2.65, label="(<0.01)**") +
  annotate("text", x=3.05, y=3.95, label="(<0.001)***") +
  annotate("text", x=2.475, y=1.90, label="(<0.001)***") +
  annotate("text", x=3.54, y=2.65, label="(<0.01)**") +
  annotate("segment", x=1.05, xend=1.05, y=2.95,yend=2.85) +
  annotate("segment", x=1.05, xend=2, y=2.85,yend=2.85) +
  annotate("segment", x=2, xend=2, y=2.85,yend=3.37) +
  annotate("segment", x=0.95, xend=0.95, y=2.95,yend=2.75) +
  annotate("segment", x=0.95, xend=2.95, y=2.75,yend=2.75) +
  annotate("segment", x=2.95, xend=2.95, y=2.75,yend=3.25) +
  annotate("segment", x=2, xend=2, y=3.63,yend=3.83) +
  annotate("segment", x=2, xend=4, y=3.83,yend=3.83) +
  annotate("segment", x=4, xend=4, y=3.83,yend=3.3) +
  annotate("segment", x=3.05, xend=3.05, y=3.25,yend=2.75) +
  annotate("segment", x=3.05, xend=4, y=2.75,yend=2.75) +
  annotate("segment", x=4, xend=4, y=2.75,yend=2.99) +
  theme(axis.text.x = element_text(size = 10, colour="black")) + 
  theme(axis.text.y = element_text(size = 9, colour="black")) + 
  theme(axis.title = element_blank())  + 
  ggtitle("Support for PM when primed with missile-attack scenarios (Lowest:1/Highest:4)")


### テロシナリオ内の比較(t検定結果)
supportPM2 <- dedn_analysis %>% 
  group_by(type) %>%
  summarise(
    support_mean = mean(rally, na.rm = TRUE),
    support_upper = mean(rally, na.rm = TRUE) + 1.96 * sd(rally, na.rm = TRUE) / sqrt(n()),
    support_lower = mean(rally, na.rm = TRUE) - 1.96 * sd(rally, na.rm = TRUE) / sqrt(n())
  )  %>% slice(-1, -2, -3, -4, -9) %>% mutate(scenario = c("Terror_Wall", "Terror_Drone", "Terror_IDF", "Terror_Regret"))
  

supportPM2 %>% ggplot(aes(as.character(type),
                         support_mean, label = round(support_mean, 2))) +
  geom_point() +
  geom_text(vjust = 0.5, hjust = -0.2, show.legend = FALSE, size = 3.5) + 
  geom_errorbar(aes(ymin = support_lower, ymax = support_upper), width = 0.1) +
  scale_x_discrete(labels = c("5" = "Terror_Wall",
                              "6" = "Terror_Drone",
                              "7" = "Terror_IDF",
                              "8" = "Terror_Regret")) +
  scale_y_continuous(breaks = seq(0, 5.0, 0.5), limits = c(2.25, 4.25)) +
  theme_bw() +
  annotate("text", x=2, y=2.7, label="(<0.001)***") +
  annotate("text", x=2.5, y=3.75, label="(<0.05)*") +
  annotate("text", x=3.5, y=2.7, label="(<0.001)***") +
  annotate("segment", x=1, xend=1, y=2.95,yend=2.85) +
  annotate("segment", x=1, xend=2.95, y=2.85,yend=2.85) +
  annotate("segment", x=2.95, xend=2.95, y=2.85,yend=3.27) +
  annotate("segment", x=2, xend=2, y=3.33,yend=3.63) +
  annotate("segment", x=2, xend=3, y=3.63,yend=3.63) +
  annotate("segment", x=3, xend=3, y=3.63,yend=3.55) +
  annotate("segment", x=3.05, xend=3.05, y=3.27,yend=2.85) +
  annotate("segment", x=3.05, xend=4, y=2.85,yend=2.85) +
  annotate("segment", x=4, xend=4, y=2.85,yend=2.89) +
  theme(axis.text.x = element_text(size = 10, colour="black")) + 
  theme(axis.text.y = element_text(size = 9, colour="black")) + 
  theme(axis.title = element_blank())  + 
  ggtitle("Support for PM when primed with terror-attack scenarios (Lowest:1/Highest:4)")
